package gov.kqqzyyy.monkey.clinicscreen.adapter;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import gov.kqqzyyy.monkey.clinicscreen.R;
import gov.kqqzyyy.monkey.clinicscreen.objects.ClinicDoctor_New;

public class DoctorAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private List<ClinicDoctor_New> doctorList;
    private int textSize = 20;
    private final Context mContext;

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    DoctorAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    void setDoctorList(List<ClinicDoctor_New> doctorList) {
        Collections.sort(doctorList, (arg0, arg1) -> arg0.getSxh() - arg1.getSxh());
        this.doctorList = doctorList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (doctorList != null) {
            return doctorList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (doctorList != null && i < doctorList.size()) {
            return doctorList.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_doctor_detail, parent, false);
            holder.doctorname = convertView.findViewById(R.id.item_doctor_name);
            holder.clinicname = convertView.findViewById(R.id.item_clinic_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ClinicDoctor_New doctor = doctorList.get(position);

//        String ysxm = doctor.getYSXM();
//        String ksxx = doctor.getPIC();
        String ysxm = doctor.getYgxm();
        String ksxx = doctor.getKsmc();
        if (null == ksxx) {
            ksxx = "";
        }
        boolean showPro = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("proficient_show", false);
        if (!showPro && !ksxx.equals("") && ksxx.contains("(")) {
            ksxx = ksxx.substring(0, ksxx.indexOf("("));
        }
        String text = ksxx.equals("") ? ysxm : ysxm + " " + ksxx;

        holder.doctorname.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

        int clinicTextSize = doctor.getCzzs().length() > 3 ? 20 : textSize;
        holder.clinicname.setTextSize(TypedValue.COMPLEX_UNIT_SP, clinicTextSize);

        holder.doctorname.setText(text);
        holder.clinicname.setText(doctor.getCzzs());
        return convertView;
    }

    private static class ViewHolder {
        private TextView doctorname;
        private TextView clinicname;
    }
}
